package org.mango.validator.fluent.common;

import com.baidu.unbiz.fluentvalidator.ValidationError;
import com.baidu.unbiz.fluentvalidator.Validator;
import com.baidu.unbiz.fluentvalidator.ValidatorContext;
import com.baidu.unbiz.fluentvalidator.ValidatorHandler;

import java.util.regex.Pattern;

/**
 * Created by meigang on 17/9/12.
 */
public class ZipCodeValidator extends ValidatorHandler<String> implements Validator<String> {
    /**
     * 匹配邮政编码
     */
    @Override
    public boolean validate(ValidatorContext context, String t) {
        if(null == t) return true;
        String reg = "[1-9]\\d{5}";
        boolean b = Pattern.matches(reg, t);
        if(b){
            return true;
        }else{
            context.addError(
                    ValidationError.create("邮政编码格式不正确")
            );
            return false;
        }

    }
}
